import positioning
import httplib, urllib
import appuifw

positioning.select_module(positioning.default_module())
positioning.set_requestors([{"type":"service",
                             "format":"application",
                             "data":"test_app"}])

#def getmyposition():
#    print 'last: ', positioning.last_position()
#    result = positioning.position()
#    print 'all gps data: ', result    
#    coordinates=result["position"]
#    mylatitude = coordinates["latitude"]
#    mylongitude = coordinates["longitude"]
#    print 'mylatitude: ', mylatitude
#    print 'mylongitude:', mylongitude
   

def senddata():
    # read the xml file from the c drive and put it into a variable called xmlfile
    #f=open('c:/xml_btscan.xml','rt')
    #xmlfile = f.read()
    #f.close()    
    call="9H1EI-2"
    comment="test"	
    mylatitude = "35.909055453539004"
    mylongitude = "14.47775998893"
    #print 'last: ', positioning.last_position()
    #result = positioning.last_position()
    #print 'all gps data: ', result    
    #coordinates=result["position"]
    #mylatitude = coordinates["latitude"]
    #mylongitude = coordinates["longitude"]
    #print 'mylatitude: ', mylatitude
    #print 'mylongitude:', mylongitude
    #define your parameters that shall be posted and assign its content: the parameter 'data' gets assigned the content of variable xmlfile   
    #params = urllib.urlencode({'data': 'eggs': 0, 'bacon': 0})
    params = urllib.urlencode({'long': mylongitude, 'lat': mylatitude, 'cl': call, 'com': comment})
    # define your headers
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    try:
	# connect to the server: put here your server URL
        conn = httplib.HTTPConnection("172.16.10.39")
        # make the post request to call the example.php file and handover the parameters and headers (put the correct folder structure here!)
        conn.request("POST", "/gps/index.php", params, headers)
        response = conn.getresponse()
	data = response.read()
	print data
        # close the connection
        conn.close()
        appuifw.note(u"Data sent", "info")
    except:
        appuifw.note(u"ok something went wrong", "info")

#getmyposition()
        
if appuifw.query(u"send APRS?","query") == True:
    senddata()


